package mmoon.mmoonstory.blocks;

import mmoon.mmoonstory.interfaces.HasModel;
import mmoon.mmoonstory.interfaces.InitBlock;
import net.minecraft.block.BlockFalling;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityFallingBlock;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import javax.annotation.Nullable;

public class BlockEngineHousing extends BlockFalling implements InitBlock, HasModel {
    public static final String ObjID = "engine_housing";
    public static final AxisAlignedBB BBoxX = new AxisAlignedBB(0, .1, .1, 1, .9, .9);
    public static final AxisAlignedBB BBoxY = new AxisAlignedBB(.1, 0, .1, .9, 1, .9);
    public static final AxisAlignedBB BBoxZ = new AxisAlignedBB(.1, .1, 0, .9, .9, 1);

    public BlockEngineHousing() {
        super(Material.ANVIL);
        this.setSoundType(SoundType.ANVIL);
        this.setHardness(1);
        this.setResistance(10);
        this.init(ObjID);
    }

    @Override
    protected void onStartFalling(EntityFallingBlock fallingEntity) {
        fallingEntity.setHurtEntities(true);
    }

    @Override
    public boolean doesSideBlockRendering(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing face) {
        return false;
    }

    @Override
    public boolean isFullCube(IBlockState state) {
        return false;
    }

    @Override
    @Nullable
    public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos) {
        switch (blockState.getValue(AXIS)) {
            case X:
                return BBoxX;
            case Z:
                return BBoxZ;
            default:
                return BBoxY;
        }
    }

    @Override
    @SideOnly(Side.CLIENT)
    public AxisAlignedBB getSelectedBoundingBox(IBlockState state, World worldIn, BlockPos pos) {
        return this.getCollisionBoundingBox(state, worldIn, pos).offset(pos);
    }

    public static final PropertyEnum<EnumFacing.Axis> AXIS = PropertyEnum.create("axis", EnumFacing.Axis.class);

    @Override
    public boolean rotateBlock(World world, BlockPos pos, EnumFacing axis) {
        IBlockState state = world.getBlockState(pos);
        for (IProperty<?> prop : state.getProperties().keySet()) {
            if (prop.getName().equals("axis")) {
                world.setBlockState(pos, state.cycleProperty(prop));
                return true;
            }
        }
        return false;
    }

    @Override
    public IBlockState withRotation(IBlockState state, Rotation rot) {
        switch (rot) {
            case COUNTERCLOCKWISE_90:
            case CLOCKWISE_90:
                switch (state.getValue(AXIS)) {
                    case X:
                        return state.withProperty(AXIS, EnumFacing.Axis.Z);
                    case Z:
                        return state.withProperty(AXIS, EnumFacing.Axis.X);
                    default:
                        return state;
                }
            default:
                return state;
        }
    }

    @Override
    public IBlockState getStateFromMeta(int meta) {
        IBlockState defaultState = this.getDefaultState();
        switch (meta & 12) {
            case 4:
                return defaultState.withProperty(AXIS, EnumFacing.Axis.X);
            case 8:
                return defaultState.withProperty(AXIS, EnumFacing.Axis.Z);
            default:
                return defaultState.withProperty(AXIS, EnumFacing.Axis.Y);
        }
    }

    @Override
    public int getMetaFromState(IBlockState state) {
        switch (state.getValue(AXIS)) {
            case X:
                return 4;
            case Z:
                return 8;
            default:
                return 0;
        }
    }

    @Override
    protected BlockStateContainer createBlockState() {
        return new BlockStateContainer(this, AXIS);
    }

    @Override
    public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY,
            float hitZ, int meta, EntityLivingBase placer) {
        return super.getStateForPlacement(worldIn, pos, facing, hitX, hitY, hitZ, meta, placer).withProperty(AXIS,
                facing.getAxis());
    }
}
